package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import com.sun.webkit.graphics.WCRenderQueue;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;


import java.util.List;

import java.util.stream.Collectors;

/**
 * @Description: hamo
 * @Create by: 渭北彭于晏
 * @Date: 2021/11/4 13:30
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;


    /**
     *新增
     * @param setmealDto
     * @return
     */
    @CacheEvict(value = "setmealCaChe",allEntries = true)
    @PostMapping
    public R save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveDto(setmealDto);
        return R.success("");
    }

    /**
     * 套餐分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String name) {

        IPage<Setmeal> pageInFor = new Page<>(page, pageSize);

        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        wrapper.orderByAsc(Setmeal::getUpdateTime);
        IPage<Setmeal> result = setmealService.page(pageInFor, wrapper);


        List<Setmeal> list = result.getRecords().stream().map((setmeal) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Category category = categoryService.getById(setmeal.getCategoryId());
            String name1 = category.getName();
            setmealDto.setCategoryName(name1);
            return setmealDto;
        }).collect(Collectors.toList());

        result.setRecords(list);

        return R.success(result);
    }

    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    @CacheEvict(value = "setmealCaChe",allEntries = true)
    @DeleteMapping
    public R deleteByIds(@RequestParam List<Long> ids) {
        setmealService.removeByIdsDto(ids);
        return R.success("删了");
    }

    /**
     * 修改状态
     *
     * @param st
     * @return
     */
    @CacheEvict(value = "setmealCaChe",allEntries = true)
    @PostMapping("/status/{st}")
    public R status(@PathVariable("st") Integer st, @RequestParam List<Long> ids) {

        UpdateWrapper<Setmeal> wrapper = new UpdateWrapper<>();
        wrapper.in("id", ids);
        wrapper.set("status", st);
        setmealService.update(wrapper);
        return R.success("状态改了");
    }

    /**
     * 数据回显
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getBuId(@PathVariable("id") Long id) {
        SetmealDto setmealDto = setmealService.getByIdDto(id);
        return R.success(setmealDto);
    }

    /**
     * 修改
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R update(@RequestBody SetmealDto setmealDto) {
        setmealService.updateDto(setmealDto);
        return R.success("改好了");
    }



    @Cacheable(value = "setmealCaChe",key = "#setmeal.categoryId+'_'+#setmeal.status")
    @GetMapping("/list")
    public R list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(wrapper);

        return R.success(list);
    }

}
